package pers.qianyu.month_202012.date_20201221;

import java.util.*;

/**
 * 232. 用栈实现队列
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 *
 * @author mizzle rain
 * @date 2020年12月21日20:31:20
 */
public class MyQueue {
    private Deque<Integer> st1 = new LinkedList<>();
    private Deque<Integer> st2 = new LinkedList<>();

    /**
     * Initialize your data structure here.
     */
    public MyQueue() {
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        st1.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        peek();
        return st2.pop();
    }

    /**
     * Get the front element.
     */
    public int peek() {
        if (st2.isEmpty()) {
            while (!st1.isEmpty()) {
                st2.push(st1.pop());
            }
        }
        return st2.isEmpty() ? -1 : st2.peek();
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return st1.isEmpty() && st2.isEmpty();
    }
}
